package com.agriculture.data_demo.repository;

import com.agriculture.data_demo.entity.sys.HenBatchEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author morningbear
 * @date 20.10.8
 * @update 20.10.19
 */
@Repository
public interface HenBatchRepository extends JpaRepository<HenBatchEntity, Integer> {

    @Query(value = "select * from hen_batch", nativeQuery = true)
    Page<HenBatchEntity> findAll(PageRequest of);

    /**
     *
     * @param shed 鸡舍
     * @return 根据鸡舍名称查找
     */
    @Query(value = "select * from hen_batch where shedName = ?1", nativeQuery = true)
    Page<HenBatchEntity> findByShed(String shed, PageRequest of);

    @Query(value = "select * from hen_batch where flkStatus = ?1", nativeQuery = true)
    Page<HenBatchEntity> findByFlkStatus(Integer flkStatus, PageRequest of);

    /**
     *
     * @return 死淘率大于平均的鸡舍
     */
    @Query(value = "select  * from hen_batch where deadRate > (select avg(deadRate) from hen_batch)", nativeQuery = true)
    Page<HenBatchEntity> findByProdRate(PageRequest of);

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截至日期
     * @return 一段时间内蛋鸡的生产批次数据
     */
    @Query(value = "select * from hen_batch where hen_batch.create_at between ?1 and ?2", nativeQuery = true)
    Page<HenBatchEntity> findHenBatchEntitiesByDate(Date startDate, Date endDate, Pageable pageable);
}
